package com.hy.dao.impl;
/*
 * @ Date: 2024/5/14 22:36
 * @ Description: CarpartstableDaoImpl
 **/

import com.hy.bean.Carpartstable;
import com.hy.dao.CarpartstableDao;
import com.hy.exception.BizException;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CarpartstableDaoImpl implements CarpartstableDao {
    private QueryRunner qr = new QueryRunner();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public Carpartstable queryById(Integer id) {
        return null;
    }

    @Override
    public List<Carpartstable> queryAllByLimit() {
        return null;
    }

    @Override
    public long count(Carpartstable carpartstable) {
        return 0;
    }

    @Override
    public int insert(Carpartstable carpartstable) {
        return 0;
    }

    @Override
    public int update(Carpartstable carpartstable) {
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }

    @Override
    public List<Carpartstable> select(String code, String beginDate, String endDate, String creator){
        Connection conn = null;
        try {
            conn = DBManager.getConnection();
        } catch (Exception e) {
            throw new BizException("建立连接出错啦",e);
        }
        StringBuilder sb = new StringBuilder("select * from carpartstable ");
        List<Object> paramList = new ArrayList<>();
        boolean isWhere = true;

        if (!StringUtils.isBlank(code)) {
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or partCode like ? ");
            }

            sb.append("partCode like ? ");
            paramList.add("%"+code+"%");
        }

        if (!StringUtils.isBlank(beginDate) && !StringUtils.isBlank(endDate)) {
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }
            sb.append(" (partCreateDate >= ? and partCreateDate<= ?)");

            paramList.add(beginDate);
            paramList.add(endDate);

        }else if(!StringUtils.isBlank(beginDate) && StringUtils.isBlank(endDate)){
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }

            sb.append(" (partCreateDate >= ? )");
            paramList.add(beginDate);
        } else if(!StringUtils.isBlank(beginDate) && StringUtils.isBlank(endDate)){
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }

            sb.append(" (partCreateDate<= ?) ");
            paramList.add(endDate);
        }

        if (!StringUtils.isBlank(creator)){
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }
            sb.append(" partCreater like ? ");
            paramList.add("%"+creator+"%");
        }

        String sql = sb.toString();
        System.out.println("sql ==================>" + sql);

        List<Carpartstable> carpartstableList = null ;
        try{
            if (paramList.size() >=1 )
                carpartstableList =  qr.query(conn, sql, new BeanListHandler<Carpartstable>(Carpartstable.class), paramList.toArray());
            else
                carpartstableList = qr.query(conn,sql,new BeanListHandler<Carpartstable>(Carpartstable.class));
        }catch (Exception e){
            throw new BizException("查询出错啦",e);
        }finally {
            try {
                DbUtils.close(conn);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }


        return carpartstableList;
    }

    @Override
    public List<Carpartstable> select(){
        Connection conn = null;
        try {
            conn = DBManager.getConnection();
            String sql = "select * from carpartstable";
            return qr.query(conn,sql,new BeanListHandler<Carpartstable>(Carpartstable.class));
        } catch (Exception e) {
            throw new BizException("Dao出错啦",e);
        }finally {
            try {
                DbUtils.close(conn);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

    }

//    public static void main(String[] args) throws Exception {
//        CarpartstableDaoImpl cdi = new CarpartstableDaoImpl();
//
//        System.out.println(cdi.select("","","",""));
//        System.out.println(cdi.select("100","","",""));
//        System.out.println(cdi.select("","2001-1-1","2005-1-1","qwe"));
//        System.out.println(cdi.select("wfpeoj","","","efwr"));
//    }
}
